package com.xiaoyu.base.utils;

import android.graphics.Bitmap;
import android.text.TextUtils;

import com.xiaoyu.base.app.AppContext;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public final class FileUtil {

    private FileUtil() {
    }

    public static boolean isFileExist(String path) {
        if (TextUtils.isEmpty(path)) {
            return false;
        }
        File file = new File(path);
        return file.exists() && file.isFile();
    }

    public static boolean isFolderExist(String path) {
        if (TextUtils.isEmpty(path)) {
            return false;
        }
        File dire = new File(path);
        return (dire.exists() && dire.isDirectory());
    }

    public static boolean deleteFile(String path) {
        if (TextUtils.isEmpty(path)) {
            return true;
        }
        File file = new File(path);
        if (!file.exists()) {
            return true;
        }
        if (file.isFile()) {
            return file.delete();
        }
        if (!file.isDirectory()) {
            return false;
        }
        for (File f : file.listFiles()) {
            if (f.isFile()) {
                f.delete();
            } else if (f.isDirectory()) {
                deleteFile(f.getAbsolutePath());
            }
        }
        return file.delete();
    }

    public static long getFileSize(String path) {
        if (TextUtils.isEmpty(path)) {
            return -1;
        }
        File file = new File(path);
        return file.exists() && file.isFile() ? file.length() : -1;
    }

    public static String getFileNameFromRemoteUrl(String url) {
        if (TextUtils.isEmpty(url) || !url.contains(".")) {
            return "";
        }
        return url.substring(url.lastIndexOf('/') + 1, url.length());
    }

    public static boolean isExistFile(String expectFileName, String folderPath) {
        if (TextUtils.isEmpty(expectFileName) || TextUtils.isEmpty(folderPath)) {
            return false;
        }
        List<String> fileNameList = listAllFileName(folderPath);
        return fileNameList.contains(expectFileName);
    }

    public static List<String> listAllFileName(String folderPath) {
        List<String> fileNameList = new LinkedList<>();
        if (TextUtils.isEmpty(folderPath)) {
            return fileNameList;
        }
        if (!isFolderExist(folderPath)) {
            return fileNameList;
        }
        File folder = new File(folderPath);
        return Arrays.asList(folder.list());
    }

    public static boolean hasAssetFile(String dir, String fileName) {
        try {
            if (AppContext.getContext() == null || AppContext.getContext().getAssets() == null) {
                return false;
            }

            final List<String> dirFileList = Arrays.asList(AppContext.getContext().getAssets().list(dir));
            if (ListUtil.isNullOrEmpty(dirFileList)) {
                return false;
            }
            return dirFileList.contains(fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static long getSizeForKB(String path) {
        File file = new File(path);
        if (file.exists()) {
            return new File(path).length() / 1024;
        }
        return 0;
    }

    public static long getSizeForKB(Bitmap bitmap) {
        return bitmap.getByteCount() / 1024;
    }
}
